<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Session;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Common\YunpianController;
class ForgetController extends Controller
{

    public function getIndex()
    {
        return view('home.forget.index');
    }

    public function postYzcode(Request $request)
    {
        $info =$request->input();
        $status =0;
        $msg = '';
        $value = $request->session()->get('yzm');
        $request->session()->put('phone', $info['phone']);
        if($info['yzm']==$value){
            $status =1;
            $msg = '验证正确';
        }else{
            $status =2;
            $msg = '手机验证码错误';
        }
        return['status'=>$status,'msg'=>$msg];

    }
    public function getChangeindex()
    {
        return view('home.forget.change');
    }
    //修改密码
    public function postChange(Request $request){
        $info = $request->input();

        $phone = $request->session()->get('phone');
        $result =Db::table('member')->where('phone',$phone)->update(['pwd'=> Hash::make($info['pwd'])]);
//        $result =Db::table('member')->where('phone',$phone)->get();
//        dd($result);
        if($result){
            //修改成功
            $status = 1;

        }else{
            //修改失败
            $status =2;

        }

        return ['status'=>$status];


    }


    // 短信发送验证码
    public function postCode(Request $request)
    {
        $phone = $request->input('mobile');
        //判断手机号是否存在
        $res = Db::table('member')->where('phone',$phone)->first();
        if($res){
            $codes = '0123456789';
            $num = '';
            for ($i=0;$i<4;$i++)
            {
                $num = $num.substr(str_shuffle($codes),0,1);
            }
            if ($phone && $num) {
                $text = "【三七物联管理平台】您的验证码是".$num;
                $yunpian = new YunpianController();
                $msg = $yunpian->Msgsend($phone, $text);
                $request->session()->put('yzm', $num);
                return $num;
            }else{
                return '';
            }
        }else{

            return ['status'=>2];
        }

    }
}
